<?php
require_once('pworks/mvc/ISystemDataHelper.iface.php');
require_once('pworks/mvc/SysDataType.class.php');

class DefaultSystemDataHelper implements ISystemDataHelper {

	//[2011-12-08][Milo Liu] Add default type for get and set functions
	public $defaultType = SysDataType::REQUEST;

	//[2011-12-08][Milo Liu] Add implement for the new interface added in ISystemDataHelper
	public function set($name, $value, $type=NULL){

		if(NULL==$type) $type = $this->defaultType;

		switch($type){
			case SysDataType::GET :
				global $_GET;
				$_GET[$name]=$value;
				break;

			case SysDataType::POST :
				global $_POST;
				$_POST[$name]=$value;
				break;

			case SysDataType::REQUEST :
				global $_REQUEST;
				$_REQUEST[$name]=$value;
				break;

			case SysDataType::SERVER :
				global $_SERVER;
				$_SERVER[$name]=$value;
				break;

			case SysDataType::ENV :
				putenv("$name=$value");
				break;

			case SysDataType::COOKIE :
				global $_COOKIE;
				$_COOKIE[$name]=$value;
				break;
		}
	}


	//[2011-12-08][Milo Liu] add default value for type parameter
	public function get($name,$type=NULL) {

		//[2011-12-08][Milo Liu] add default value for type parameter
		if(NULL==$type) $type = $this->defaultType;


		$retVal = null;
		switch($type){
			case SysDataType::GET :
				global $_GET;
				if(array_key_exists($name, $_GET)){
					$retVal = $_GET[$name];				
				}
				break;

			case SysDataType::POST :
				global $_POST;
				if(array_key_exists($name, $_POST)){
					$retVal = $_POST[$name];				
				}
				break;

			case SysDataType::REQUEST :
				global $_REQUEST;
				if(array_key_exists($name, $_REQUEST)){
					$retVal = $_REQUEST[$name];				
				}
				break;

			case SysDataType::SERVER :
				global $_SERVER;
				if(array_key_exists($name, $_SERVER)){
					$retVal = $_SERVER[$name];				
				}
				break;

			case SysDataType::ENV :
				if(getenv($name)){
					$retVal = getenv($name);			
				}
				break;

			case SysDataType::COOKIE :
				global $_COOKIE;
				if(array_key_exists($name, $_COOKIE)){
					$retVal = $_COOKIE[$name];				
				}
				break;

		}
		return $retVal;
	}
}

